/////////////////////////////////////////////////////////////////////////////
// Name:        mp3encoder.h
// Purpose:     A wxWidgets wrapper around LAME
// Author:      Julian Smart
// Modified by: 
// Created:     2009-02-10
// RCS-ID:      
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#ifndef _MP3ENCODER_H_
#define _MP3ENCODER_H_

/*
 * Includes
 */

#include "wx/dynarray.h"

/*
 * Styles and types
 */

// MPEG1 format
#define wxMP3ENCODER_OPTION_MPEG1   0x01

// MPEG2 format
#define wxMP3ENCODER_OPTION_MPEG2   0x02

// Include ID3 information
#define wxMP3ENCODER_OPTION_ID3     0x04

// Quiet operation
#define wxMP3ENCODER_OPTION_QUIET   0x08

/**
    @class wxID3Info
    
    Stores MP3 ID3 information.

    @category{tts}
 */

class wxID3Info
{
public:
    wxID3Info() { Init(); }
    wxID3Info(const wxID3Info& info) { Init(); Copy(info); }

    void operator=(const wxID3Info& info) { Copy(info); }
    bool operator==(const wxID3Info& info) const;
    void Copy(const wxID3Info& info);
    void Init();

    void SetTitle(const wxString& title) { m_title = title; }
    const wxString& GetTitle() const { return m_title; }

    void SetArtist(const wxString& artist) { m_artist = artist; }
    const wxString& GetArtist() const { return m_artist; }

    void SetAlbum(const wxString& album) { m_album = album; }
    const wxString& GetAlbum() const { return m_album; }

    void SetYear(const wxString& year) { m_year = year; }
    const wxString& GetYear() const { return m_year; }

    void SetComment(const wxString& comment) { m_comment = comment; }
    const wxString& GetComment() const { return m_comment; }

    void SetGenre(const wxString& genre) { m_genre = genre; }
    const wxString& GetGenre() const { return m_genre; }

    void SetTrackNumber(int trackNumber) { m_trackNumber = trackNumber; }
    int GetTrackNumber() const { return m_trackNumber; }

    void SetTrackCount(int trackCount) { m_trackCount = trackCount; }
    int GetTrackCount() const { return m_trackCount; }

protected:
    wxString    m_title;
    wxString    m_artist;
    wxString    m_album;
    wxString    m_year;
    wxString    m_comment;
    wxString    m_genre;
    int         m_trackNumber;
    int         m_trackCount;
};

WX_DECLARE_OBJARRAY(wxID3Info, wxID3InfoArray);

/**
    @class wxMP3EncoderInfo
    
    Stores MP3 encoding information.

    @category{tts}
 */

class wxMP3EncoderInfo
{
public:
    wxMP3EncoderInfo() { Init(); }

    void Init() { m_enabled = true; m_bitRate = 128; }
    void Copy(const wxMP3EncoderInfo& info)
    { m_enabled = info.m_enabled; m_encoderPath = info.m_encoderPath; m_bitRate = info.m_bitRate; }
    void operator=(const wxMP3EncoderInfo& info) { Copy(info); }

    bool                m_enabled;
    int                 m_bitRate;
    wxString            m_encoderPath;
};

/**
    @class wxMP3Encoder
    
    Handles MP3 encoding using LAME.

    @category{tts}
 */

class wxMP3EncoderProcess;

class wxMP3Encoder: public wxEvtHandler
{    
    DECLARE_DYNAMIC_CLASS( wxMP3Encoder )

public:
// Constructors

    wxMP3Encoder();
    ~wxMP3Encoder();

// Operations

    /// Member initialisation
    void Init();

    /// Encode the WAV file as MP3
    virtual bool Encode(const wxString& inputFilename, const wxString& outputFilename, int options = 0);

    /// Is it encoding?
    virtual bool IsEncoding() { return (GetProcess() != NULL); }

    /// Stop encoding
    virtual bool Stop();

    /// Set/get the options
    void SetOptions(int options) { m_options = options; }
    int GetOptions() const { return m_options; }

    /// Set/get info
    void SetInfo(const wxMP3EncoderInfo& info) { m_info = info; }
    const wxMP3EncoderInfo& GetInfo() const { return m_info; }

    /// Set/get the program location
    void SetProgramLocation(const wxString& location) { m_info.m_encoderPath = location; }
    const wxString& GetProgramLocation() const { return m_info.m_encoderPath; }

    /// Set/get extra switches
    void SetExtraSwitches(const wxString& switches) { m_extraSwitches = switches; }
    const wxString& GetExtraSwitches() const { return m_extraSwitches; }

    /// Set/get the ID3 data
    void SetID3(const wxID3Info& info) { m_id3Info = info; }
    const wxID3Info& GetID3Info() const { return m_id3Info; }

    /// Set/get the bitrate
    void SetBitRate(int bitRate) { m_info.m_bitRate = bitRate; }
    int GetBitRate() const { return m_info.m_bitRate; }

    /// Set/get the last command
    void SetLastCommand(const wxString& command) { m_command = command; }
    const wxString& GetLastCommand() const { return m_command; }

    /// Set/get the error code
    void SetErrorCode(int errorCode) { m_errorCode = errorCode; }
    int GetErrorCode() const { return m_errorCode; }

    /// Set/get the process id
    void SetPID(long pid) { m_pid = pid; }
    long GetPID() const { return m_pid; }

    /// Set/get the output from espeak
    void SetOutput(const wxArrayString& output) { m_output = output; }
    const wxArrayString& GetOutput() const { return m_output; }

    /// Add a line of output
    void WriteOutput(const wxString& output) { m_output.Add(output); }

    /// Process any pending input
    bool ProcessInput();

    /// Set/get process
    void SetProcess(wxMP3EncoderProcess* process) { m_process = process; }
    wxMP3EncoderProcess* GetProcess() const { return m_process; }

protected:
    int                 m_options;          // remember the options

    wxMP3EncoderInfo    m_info;
    //wxString            m_programLocation;
    //wxString            m_voice;
    wxString            m_command;
    wxString            m_extraSwitches;
    wxArrayString       m_output;
    //int                 m_bitRate;
    wxID3Info           m_id3Info;
    int                 m_errorCode;
    wxMP3EncoderProcess* m_process;
    long                m_pid;
};

#endif
    // _MP3ENCODER_H_

